_package("alz.mui");

_import("alz.mui.Pane");
_import("alz.mui.Tab");
_import("alz.mui.Table");
_import("alz.mui.ScrollView");

/**
 * 调试用面板组件
 */
_class("PaneDebug", Pane, function(){
	//<div class="button" onclick="window.location.reload();return false;">刷新</div>
	this._init = function(){
		_super._init.call(this);
		this._tabs = [];
		this._activeTab = null;
		this._logNum = 0;
		this._tables = [];
		this._closed = false;
		this._scrollview = null;
	};
	this.create = function(parent, app){
		this.setParent(parent);
		this._app = app;
		var obj = this.createTplElement(parent, "pane_debug.xml");
		this.init(obj);
		return obj;
	};
	this.init = function(obj){
		_super.init.apply(this, arguments);
		if(runtime._platform == "android"){
			this.setOpacity(0.8);
		}
		var tabs = this._self.childNodes[0].childNodes;
		var nodes = this._self.childNodes;
		for(var i = 0, len = tabs.length - 2; i < len; i++){
			var tab = new Tab();
			tab.bind(tabs[i], nodes[i + 1]);
			this._tabs.push(tab);
		}
		nodes = null;
		tabs = null;
		this._scrollview = new ScrollView();
		this._scrollview.bind($E("log"), {
			"parent": this,
			"id"    : "scroll1",
			"scrollmode": "normal"
		});
		this.initActionElements(this._self, null, ["li"]);  //初始化动作元素
		this.activeTab(this._tabs[0]);
		this.find("input[name=\"network\"]").checked = !runtime._offline;
		this.setClosed(true);
	};
	this.dispose = function(){
		this._scrollview.dispose();
		this._scrollview = null;
		for(var i = 0, len = this._tables.length; i < len; i++){
			this._tables[i].dispose();
			this._tables[i] = null;
		}
		this._tables = [];
		this._activeTab = null;
		for(var i = 0, len = this._tabs.length; i < len; i++){
			this._tabs[i] = null;
		}
		this._tabs = [];
		_super.dispose.apply(this);
	};
	this.activeTab = function(v){
		if(this._activeTab == v) return;
		if(this._activeTab){
			this._activeTab.deactivate();
		}
		v.activate();
		this._activeTab = v;
	};
	this.appendLog = function(str, type){
		var parent = this._scrollview._scroller;
		if(parent){
			var row;
			if(this._logNum >= 50){
				row = parent.removeChild(parent.firstChild);
				parent.appendChild(row);
			}else{
				row = this.createElement(parent, "li", "row");
			}
			var d = new Date();
			var time = d.getMinutes() + ":" + d.getSeconds() + ":" + d.getMilliseconds();
			row.innerHTML = '[' + time + ']<span class="' + type + '">' + runtime.encodeHTML(str) + '</span>';
			this._logNum++;
		}
	};
	this.updateHistory = function(arr){
		$E("history1").value = arr.join("\n");
	};
	this.createPageListItem = function(data, app){
		var li = this._createElement("li");
		var a = this._createElement("a");
		a.href = "#";
		a.setAttribute("_pid", data.pid);
		a.onclick = function(ev){
			app.navPage(this.getAttribute("_pid"));
			return false;
		};
		a.innerHTML = data.tpl;
		li.appendChild(a);
		$E("dev1").appendChild(li);
	};
	/**
	 * 显示速率变化表
	 */
	this.showTable = function(list){
		var table = new Table();
		table.create(this._tabs[3]._page, list);
		this._tables.push(table);
		return table.getTotalData();
	};
	this.setClosed = function(v){
		if(this._closed == v) return;
		this._closed = v;
		if(!v){
			this._self.style.left = "0px";
			//this._self.style.bottom = "0px";
			this._self.style.height = "300px";
		}else{
			this._self.style.left = "-328px";
			//this._self.style.bottom = "-265px";
			this._self.style.height = "37px";
		}
		this.find(".right").innerHTML = !v ? "收起" : "展开";
	};
	this.do_show_page = function(act, sender){
		var id = parseInt(sender.getAttribute("_id"));
		this.activeTab(this._tabs[id]);
	};
	this.do_refresh = function(act, sender){
		window.location.reload();
	};
	this.do_close = function(act, sender){
		this.setClosed(!this._closed);
	};
	this.do_network = function(act, sender){
		runtime.setOffline(!sender.checked);
		return true;
	};
	/**
	 * 应用暂停(划屏离开)
	 */
	this.do_app_suspend = function(act, sender){
		runtime.fireEvent({"type": "suspend"});
	};
	/**
	 * 应用恢复(划屏回来)
	 */
	this.do_app_resume = function(act, sender){
		runtime.fireEvent({"type": "resume"});
	};
});